package com.jerei.plugin.control;

import com.alibaba.fastjson.JSONArray;
import com.jerei.plugin.entity.WcmCommonMenuDetail;
import com.jerei.plugin.service.IWcmCommonMenuDetailService;
import com.jerei.utils.UtilsCommon;
import com.jerei.web.plugin.controller.PluginController;
import com.jerei.web.utils.UtilsUI;
import com.jerei.web.utils.UtilsWeb;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import ro.fortsoft.pf4j.Extension;

/**
 * Created by sunfeihu on 2016/10/21.
 */
@Extension
public class WcmCommonMenuDetailControl extends PluginController {

    //业务逻辑层
    @Autowired
    private IWcmCommonMenuDetailService iWcmCommonMenuDetailService;

    /**
     * 保存和修改数据
     *
     * @return 保存结果
     */
    @RequiresPermissions({"common-menu:save:*", "common-menu:update:*"})
    public String save() {
        WcmCommonMenuDetail entity = this.wrapper(WcmCommonMenuDetail.class);
        if (entity.getId() != null && entity.getId() > 0L) {
            iWcmCommonMenuDetailService.update(entity);
        } else {
            iWcmCommonMenuDetailService.save(entity);
        }
        return UtilsUI.ok();
    }

    @RequiresPermissions({"common-menu:save:*", "common-menu:update:*", "common-menu:view:*"})
    public void edit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        WcmCommonMenuDetail entity = null;
        if (null != id && id > 0L) {
            entity = iWcmCommonMenuDetailService.find(id);
        } else {
            Long pid = UtilsWeb.getLong(this.getRequest(), "pid");
            entity = new WcmCommonMenuDetail();
            entity.setMenuNo(1);
            entity.setIsShow(1);
            if (null != pid && pid > 0) {
                entity.setPid(pid);
            }
        }
        this.getModel().addAttribute("obj", entity);
    }

    /**
     * 数据删除
     *
     * @return 删除结果
     */
    @RequiresPermissions({"common-menu:remove:*"})
    public String delete() {
        String[] ids = this.getRequest().getParameterValues("ids");
        if (null != ids && ids.length > 0) {
            for (int i = 0; i < ids.length; i++) {
                iWcmCommonMenuDetailService.delete(UtilsCommon.getLong(ids[i]));
            }
            return UtilsUI.ok();
        }
        return UtilsUI.fail();
    }

    @RequiresPermissions({"common-menu:view:*"})
    public String rightPage() {
        Integer nowPage = UtilsWeb.getInteger(this.getRequest(), "page");
        Integer pageSize = UtilsWeb.getInteger(this.getRequest(), "rows");
        Long pid = UtilsWeb.getLong(this.getRequest(), "search.eq.pid");
        if (null != pid && pid > 0) {
            JSONArray array = iWcmCommonMenuDetailService.treeMenuDetail(pid);
            if (null != array) {
                return array.toJSONString();
            } else {
                return new JSONArray().toJSONString();
            }
        } else {
            return new JSONArray().toJSONString();
        }
    }

    @RequiresPermissions({"common-menu:view:*"})
    public String leftPage() {
        Integer nowPage = UtilsWeb.getInteger(this.getRequest(), "page");
        Integer pageSize = UtilsWeb.getInteger(this.getRequest(), "rows");
        return UtilsUI.datagrid(iWcmCommonMenuDetailService.devRootMenuDetail(nowPage, pageSize));
    }
}